#include "mei_file.h"
#include <thread>
#include <iostream>
#include <windows.h>
#include <array>
#include <vector>
#include <functional>
#include <fstream>
#include <iomanip>
#include "ThreadPool.h"

//const char *send_path = "G:\\download\\doinaka_brother.zip";
//const char *recv_path = "G:\\download\\doinaka.zip";
//const char *send_path = "G:\\download\\com.huawei.hwid.apk";
//const char *recv_path = "G:\\download\\hwid.apk";
//const char *send_path = "G:\\download\\source";
//const char *recv_path = "G:\\download\\result";
const char *send_path = "F:\\workspace\\pycharm\\小魅二代\\testcase\\data\\sql_backup\\20250519.sql";
const char *recv_path = "result";
const char *host = "127.0.0.1";
const int port = 99;

int main() {
    auto start = std::chrono::high_resolution_clock::now();

    std::thread t_send(sendFile, send_path, host, port);
    std::thread t_recv(recvFile, recv_path, host, port);
    t_send.join();
    t_recv.join();

    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    std::cout << "函数执行时间: " << duration.count() << " 毫秒" << std::endl;
    return 0;


    HMODULE hModule = LoadLibrary("lib_mei_file.dll");
    if (!hModule) {
        std::cerr << "Failed to load DLL" << std::endl;
        return 1;
    }

    auto sendFilePtr = reinterpret_cast<fileFunPtr>(GetProcAddress(hModule, "sendFile"));
    auto recvFilePtr = reinterpret_cast<fileFunPtr>(GetProcAddress(hModule, "recvFile"));
    if (!sendFilePtr || !recvFilePtr) {
        std::cerr << "Failed to get function address" << std::endl;
        FreeLibrary(hModule);
        return 1;
    }

    std::thread th_send(sendFilePtr, send_path, host, port);
    std::thread th_recv(recvFilePtr, recv_path, host, port);
    th_send.join();
    th_recv.join();

    FreeLibrary(hModule);
    return 0;
}

int f(int a, int b) {
    return a + b;
}

int main2() {
    ThreadPool<4> p;
    std::array<int, 3> a{1, 2, 3};
    std::vector<int> b{1, 2, 3};
    std::function<int(int, int)> g = f;
    std::vector<std::tuple<int, int>> c{{1, 2},
                                        {3, 4}};
    auto res = p.map(unpackFun(g), c);
    for (const auto &x: res) {
        std::cout << x->get_future().get() << std::endl;
    }
    auto res2 = ThreadPool<1>().map([](int x) { return x; }, std::vector<int>{1, 2, 3});
    for (const auto &x: res2) {
        std::cout << x->get_future().get() << std::endl;
    }
    auto res3 = ThreadPool<1>().map([](const int &x) { return x; }, std::vector<int>{1, 2, 3});
    for (const auto &x: res3) {
        std::cout << x->get_future().get() << std::endl;
    }
    std::vector<int> temp;
    int y = 0;
//    ThreadPool<12>().each([&](int x) {
//        std::ofstream file;
//        file.open(std::to_string(x));
//        for (int i = 0; i < x * 1000000000; i++) {
//            y += x;
////            std::cout << x << " " << std::this_thread::get_id() << " " << i << std::endl;
////            std::this_thread::sleep_for(std::chrono::milliseconds(1));
////            temp.push_back(x);
//            file << x;
//        }
//    }, std::views::iota(0) | std::views::take(24));
    for (auto x: temp) {
        std::cout << x << " ";
    }
    std::cout << y << std::endl;
    return 0;
}

int main3() {
    auto f = [](int x) {
        for (int i = 0; i < x * 10; i++) {
            std::cout << x << " " << std::this_thread::get_id() << " " << i << std::endl;
        }
        return 0;
    };
    std::thread ts[3];
    for (int i = 1; i <= 3; i++) {
        ts[i - 1] = std::thread(f, i);
    }
    for (auto &t: ts) {
        if (t.joinable()) {
            t.join();
        }
    }
    return 0;
}